package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nfcs.NfcsData;
import com.ucredit.babel.common.model.nfcs.NfcsReport;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Classname ShangHaiNfcsReportApplication
 * @Description 上海资信报告-贷款申请&担保&特殊交易信息
 * @Date 2021/5/27 23:03
 * @Created by lzw
 */
public class ShangHaiNfcsReportApplication extends ParseBase {
    private Text key = new Text();
    private Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {

        NfcsReport obj = (NfcsReport) model;
        NfcsData data = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = new StringBuilder(500);

        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());

        JSONObject loanApplicationInformation = info.getString("loanApplicationInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("loanApplicationInformation"));
        JSONObject guaranteeInformation = info.getString("guaranteeInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("guaranteeInformation"));
        JSONObject specialTransactionInformation = info.getString("specialTransactionInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("specialTransactionInformation"));

        JSONArray loanApplicationRecords = loanApplicationInformation.getString("loanApplicationRecords") == null ? new JSONArray() : loanApplicationInformation.getJSONArray("loanApplicationRecords");
        JSONArray guaranteeInformationRecords = guaranteeInformation.getString("guaranteeInformationRecords") == null ? new JSONArray() : guaranteeInformation.getJSONArray("guaranteeInformationRecords");
        JSONArray detailRecords = specialTransactionInformation.getString("detailRecords") == null ? new JSONArray() : specialTransactionInformation.getJSONArray("detailRecords");

        if (!loanApplicationRecords.isEmpty()) {
            for (int i = 0; i < loanApplicationRecords.size(); i++) {
                JSONObject loanApplicationRecord = loanApplicationRecords.getString(i) == null ? new JSONObject() : JSON.parseObject(loanApplicationRecords.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(loanApplicationRecord.getString("applicationAgency") == null ? "" : loanApplicationRecord.getString("applicationAgency")).append(CRT001);
                txSb.append(loanApplicationRecord.getString("applicationTime") == null ? "" : loanApplicationRecord.getString("applicationTime")).append(CRT001);
                txSb.append(loanApplicationRecord.getString("applicationAmount") == null ? "" : loanApplicationRecord.getString("applicationAmount")).append(CRT001);
                txSb.append(loanApplicationRecord.getString("applicationMonthNo") == null ? "" : loanApplicationRecord.getString("applicationMonthNo")).append(CRT001);
                txSb.append(loanApplicationRecord.getString("applicationType") == null ? "" : loanApplicationRecord.getString("applicationType")).append(CRT001);
                txSb.append(loanApplicationRecord.getString("applicationStatus") == null ? "" : loanApplicationRecord.getString("applicationStatus")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(loanApplicationRecord.getString("getDate") == null ? "" : loanApplicationRecord.getString("getDate")).append(CRT001);

                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }


        if (!guaranteeInformationRecords.isEmpty()) {
            for (int i = 0; i < guaranteeInformationRecords.size(); i++) {
                JSONObject guaranteeInformationRecord = guaranteeInformationRecords.getString(i) == null ? new JSONObject() : JSON.parseObject(guaranteeInformationRecords.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(CRT001);
                txSb.append(guaranteeInformationRecord.getString("guaranteeDate") == null ? "" : guaranteeInformationRecord.getString("guaranteeDate")).append(CRT001);
                txSb.append(guaranteeInformationRecord.getString("guaranteeAmount") == null ? "" : guaranteeInformationRecord.getString("guaranteeAmount")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(guaranteeInformationRecord.getString("guaranteeProject") == null ? "" : guaranteeInformationRecord.getString("guaranteeProject")).append(CRT001);
                txSb.append(guaranteeInformationRecord.getString("guaranteeRelationship") == null ? "" : guaranteeInformationRecord.getString("guaranteeRelationship")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(guaranteeInformationRecord.getString("getDate") == null ? "" : guaranteeInformationRecord.getString("getDate")).append(CRT001);

                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }


        if (!detailRecords.isEmpty()) {
            for (int i = 0; i < detailRecords.size(); i++) {
                JSONObject detailRecord = detailRecords.getString(i) == null ? new JSONObject() : JSON.parseObject(detailRecords.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(CRT001);
                txSb.append(detailRecord.getString("inDate") == null ? "" : detailRecord.getString("inDate")).append(CRT001);
                txSb.append(detailRecord.getString("inAmount") == null ? "" : detailRecord.getString("inAmount")).append(CRT001);
                txSb.append(detailRecord.getString("changeMonth") == null ? "" : detailRecord.getString("changeMonth")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(detailRecord.getString("recordSource") == null ? "" : detailRecord.getString("recordSource")).append(CRT001);
                txSb.append(detailRecord.getString("recordType") == null ? "" : detailRecord.getString("recordType")).append(CRT001);
                txSb.append(detailRecord.getString("detailInformation") == null ? "" : detailRecord.getString("detailInformation")).append(CRT001);
                txSb.append(detailRecord.getString("getDate") == null ? "" : detailRecord.getString("getDate")).append(CRT001);

                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_nfcs_report_application_inc";
    }

    @Override
    public String getSubType() {
        return ".application";
    }

    @Override
    public String getType() {
        return RecordType.NFCS_REPORT.toString();
    }

    @Override
    public String getComment() {
        return "上海资信报告-贷款申请&担保&特殊交易信息";
    }
}
